清除所有變數資料
rm(list = ls(all = TRUE))
導入所需套件
library(quantmod)
library(ggplot2)
library(plotly)
讀S&P500資料
sp500 = getSymbols("^GSPC", src = "yahoo", auto.assign = FALSE , from = '1990-01-01', to = '2018-08-31')
as.data.frame(sp500[1:10 , ]) %>%
kable(digits = 2 , align = 'c' ) %>%
kable_styling(bootstrap_options = c("striped", "hover"))
| GSPC.Open | GSPC.High | GSPC.Low | GSPC.Close | GSPC.Volume | GSPC.Adjusted | |
|---|---|---|---|---|---|---|
| 1990-01-02 | 353.40 | 359.69 | 351.98 | 359.69 | 162070000 | 359.69 |
| 1990-01-03 | 359.69 | 360.59 | 357.89 | 358.76 | 192330000 | 358.76 |
| 1990-01-04 | 358.76 | 358.76 | 352.89 | 355.67 | 177000000 | 355.67 |
| 1990-01-05 | 355.67 | 355.67 | 351.35 | 352.20 | 158530000 | 352.20 |
| 1990-01-08 | 352.20 | 354.24 | 350.54 | 353.79 | 140110000 | 353.79 |
| 1990-01-09 | 353.83 | 354.17 | 349.61 | 349.62 | 155210000 | 349.62 |
| 1990-01-10 | 349.62 | 349.62 | 344.32 | 347.31 | 175990000 | 347.31 |
| 1990-01-11 | 347.31 | 350.14 | 347.31 | 348.53 | 154390000 | 348.53 |
| 1990-01-12 | 348.53 | 348.53 | 339.49 | 339.93 | 183880000 | 339.93 |
| 1990-01-15 | 339.93 | 339.94 | 336.57 | 337.00 | 140590000 | 337.00 |
取出close資料
sp500 = sp500$GSPC.Close
#sp500 = sp500$GSPC.Close
##若變數中有空格,欄位名稱加''
#ex:GSPC Low
#sp500$'GSPC Low'
改名字
colnames(sp500) = 'sp500'
設定時間
start = "1990-01-01"
end = "2018-08-31"
一年債券
one_y = getSymbols("DGS1", src = "FRED", auto.assign = FALSE)
colnames(one_y) = 'bond_one'
十年債券
ten_y = getSymbols("DGS10", src = "FRED", auto.assign = FALSE)
colnames(ten_y) = 'bond_ten'
資料合併(xts)
database = cbind(sp500 , one_y , ten_y)
#cbind( = merge)、rbind
去除na
database = na.omit(database)
取出index
time = index(database)
#直接放入dataframe
#database$time = index(database)
ggplot2畫圖
g = ggplot(data = database , aes(x = time)) +
geom_line(aes( y = sp500, colour = "S&P 500")) +
geom_line(aes( y = bond_one*447, colour = "One Year Bond")) +
geom_line(aes( y = bond_ten*447, colour = 'Ten Years Bond'))+
scale_y_continuous(sec.axis = sec_axis(~.*(1/447) ))
g
plotly畫圖
#將database轉為dataframe格式
database = as.data.frame(database)
p = plot_ly(data = database , x = time) %>%
#plotly中database$sp500,也可寫成~sp500
add_lines(y = database$sp500 , type = "scatter" , mode = "lines" ,
line = list(color = 'gray') , name = "S&P 500") %>%
add_lines(y = database$bond_one ,
type = "scatter" , mode = "lines" ,
line = list(color = 'cornflowerblue') ,
yaxis = "y2", name = "One-Year Bond" ) %>%
add_lines(y = database$bond_ten ,
type = "scatter" , mode = "lines",
line = list(color = 'aquamarine') ,
yaxis = "y2", name = 'Ten-Year Bond' ) %>%
layout(title = 'S&P 500 v.s. Bond',
xaxis = list(
rangeselector = list(
buttons = list(
list(
count = 3,
label = "3m",
step = "month",
stepmode = "backward"),
list(
count = 6,
label = "6m",
step = "month",
stepmode = "backward"),
list(
count = 1,
label = "1y",
step = "year",
stepmode = "backward"),
list(
count = 3,
label = "3y",
step = "year",
stepmode = "backward"),
list(
count = 5,
label = "5y",
step = "year",
stepmode = "backward"),
list(step = "all"))),
rangeslider = list(type = "date")),
yaxis = list(side = 'left' ,
title = 'S&P 500 Index') ,
yaxis2 = list(title = 'Bond Yield (%)' ,
overlaying = "y", side = "right"))
offline( p )
## Warning: 'offline' is deprecated.
## Use 'plot_ly' instead.
## See help("Deprecated")
資料視覺化比較
#ggplot2
g
#plotly畫ggplot2(第二個y軸會消失)
ggplotly(g)
#plotly
offline( p )
#存為html檔
htmlwidgets::saveWidget( p , 'Bond Fig.html')